﻿using RobotPratolSolution.ViewModels;
using System.Windows;
using System.Windows.Controls;

namespace RobotPratolSolution.Pages
{
    /// <summary>
    /// StationMange.xaml 的交互逻辑
    /// </summary>
    public partial class StationMange : UserControl
    {
        public StationMangeViewModel stationMangeViewModel { get; set; }
        public StationMange()
        {
            InitializeComponent();
            stationMangeViewModel = new StationMangeViewModel();
            this.DataContext = stationMangeViewModel;
        }




        private void UpdateBtn_Click(object sender, RoutedEventArgs e)
        {
            int index = StationListBox.SelectedIndex;
            if (index >= 0)
            {
                stationMangeViewModel.UpdateStation(index);
            }
        }

        private void DeleteBtn_Click(object sender, RoutedEventArgs e)
        {
            int index = StationListBox.SelectedIndex;
            if (index >= 0)
            {
                stationMangeViewModel.DeleteStation(index);
            }
        }

        private void Addbtn_Click(object sender, RoutedEventArgs e)
        {
            string name = PointNameTextBox.Text;
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("点位名称不能为空！");
            }
            else
            {
                stationMangeViewModel.AddStation(name, PointTypeComBox.Text);
            }

        }

        private void Refreshbtn_Click(object sender, RoutedEventArgs e)
        {
            stationMangeViewModel.RefreshStation();
        }



        private void StationListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = StationListBox.SelectedIndex;
            if (index >= 0)
            {
                var data = stationMangeViewModel.StationModels[index];

                stationMangeViewModel.PointName = data.Name;
                stationMangeViewModel.PointX = data.X;
                stationMangeViewModel.PointY = data.Y;
                stationMangeViewModel.PointZ = data.Z;
                stationMangeViewModel.PointType = data.PointType;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            stationMangeViewModel.RefreshStation();
            stationMangeViewModel.AddSubTopic();

            StationListBox.ItemsSource = stationMangeViewModel.StationModels;
        }

        private void GetPointbtn_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(stationMangeViewModel.PointType))
            {
                MessageBox.Show("请选择点位类型！");
                return;
            }
            stationMangeViewModel.StationGetPoint();
        }

        private void DoChargeBtn_Click(object sender, RoutedEventArgs e)
        {
            stationMangeViewModel.DoChargeFunc();
        }

        private void MoveBtn_Click(object sender, RoutedEventArgs e)
        {

            stationMangeViewModel.DoMovePoint(0);

        }
        private void MoveXYBtn_Click(object sender, RoutedEventArgs e)
        {

            stationMangeViewModel.DoMovePoint(1);


        }

        private void MoveAngleBtn_Click(object sender, RoutedEventArgs e)
        {
            var index = StationListBox.SelectedIndex;
            if (index >= 0)
            {
                stationMangeViewModel.DoMovePoint(2);
            }

        }

        private void UpdateToRobotbtn_Click(object sender, RoutedEventArgs e)
        {

            stationMangeViewModel.DoUpdateToRobot();

        }
    }
}
